﻿using System;
using System.Threading;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Jannette Hamilton
////Date created: June 19, 2014
////Modified by: 
////Date modified: 
////Reason: CONVERTED FROM SWAT: SuiteCompliance.SuiteDotNet.SuiteWorkersCompensation.SuiteGuiTests.TestWorkersCompRiskRatesGui
////Run Time: ~6mins (3 BROWSERS)
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.WorkersComp
{
    [TestFixture]
    [Author("Jannette Hamilton", "Jannette_Hamilton@ultimatesoftware.com")]
    [Category("BusinessRules: Workers Compensation Risk Rates")]
    [Header("Config")]
    [Config(".NET")]
    //[Config("IEOnly")]
    //[Config("ChromeOnly")]
    //[Config("FFOnly")]

    public class WorkerCompRiskRateGuiRedone : MbUnitAutomatedTest
    {
        private UltiPro UltiPro;
        private UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");

            // GIVEN THAT A SYSTEM ADMIN LOGS IN AND NAVIGATES TO Workers' Compensation Risk Rates BUSINESS RULE
            UltiPro.Pages.Login.LoginAs("AtnipJ", "password", Config.LanguageCode);
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
            Database.Company.Execute("DELETE FROM Codes WHERE CodCode = 'WCRR'");
            Database.Company.Execute("Delete from wcrisk where wcrwccode =  'WCRR'");
            Thread.Sleep(5000); // WAIT A FEW SECS BEFORE THE INSERT
        }

        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        #region Helper Method
        private void SelectWorkersCompRiskRateCode(string WcRiskRateCode)
        {
            UltiPro.Macros.Navigate.ToBusinessRule("Workers' Compensation Risk Rates"); 
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Find(code => code.CodeFilter, FilterType.Is, WcRiskRateCode);
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Row(column => column.Code, WcRiskRateCode).Click(x => x.Code);
        }
        #endregion Helper Method

        [Test(Order = 1)]
        public void VerifyWorkersCompRiskRateGridGui()
        {
            UltiPro.Macros.Navigate.ToBusinessRule("Workers' Compensation Risk Rates");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.CodeHeader, "Code");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.DescriptionHeader, "Description");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.StateHeader, "State");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.CompanyHeader, "Company");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.EffectiveDateHeader, "Effective Date");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.EmployeeRateHeader, "Employee Rate");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.EmployerRateHeader, "Employer Rate");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.ModifierHeader, "Modifier");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Like(header => header.TotalRateHeader, "Total Rate");
        }

      [Test(Order = 2)]
        public void InsertRiskRateAndVerifyGui()
        {
          // INSERT WORKERS COMP CODE AND RISK RATE  
            Database.Company.Execute("INSERT INTO Codes (CodCode, CodDesc, CodTable, CodCountryCode) VALUES ('WCRR', 'Workers Comp Code Description', 'WCCODES', 'USA')");
            Database.Company.Execute("INSERT into WCRisk(WcrCoID, WcrDatetimeCreated, WcrEERiskRate, WcrEffDate, WcrEffStopDate, WcrERRiskRate, WcrHasBeenReplaced, WcrState, WcrSystemID, WcrWCCode, WcrWcModifier) values('49M7T',GetDate(), .01, '20080215 00:00:00.000', NULL, .02, 'N', 'FL', dbo.fn_GetTimedKey(), 'WCRR', 1.5)");
            
            // SELECT THE NEW CODE IN BUSINESS RULES AND VERIFY THE DATA   
            SelectWorkersCompRiskRateCode("WCRR"); 
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "FL - Florida");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "WCRR - Workers Comp Code Description");
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CompanyValue, "innerHTML", "US Payroll Company");
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CurrentEffectiveDateValue, "innerHTML", "2/15/2008");
            //UltiPro.Pages.WcRiskAddChg.Like(page => page.NewEffectiveDateValue, "innerHTML", DateTime.Today.ToShortDateString()); // SEES ""
            UltiPro.Pages.WcRiskAddChg.Is(dDown => dDown.InsuranceCarrierDropDown, "innerHTML", "");
            UltiPro.Pages.WcRiskAddChg.Like(textBox => textBox.EmployeeRateTextBox, "value", "1.00 %");
            UltiPro.Pages.WcRiskAddChg.Like(textBox => textBox.EmployerRateTextBox, "value", "2.00 %");
            UltiPro.Pages.WcRiskAddChg.Like(textBox => textBox.ModifierTextBox, "value", "1.50");
            UltiPro.Pages.WcRiskAddChg.Like(textBox => textBox.TotalRateTextBox, "innerHTML", "4.00");
        }

        [Test(Order = 3)]
        // ROW MUST HAVE BEEN INSERTED IN 2 ABOVE. NOT USING DEPENDS_ON B/C RATES MAY DISPLAY FUNNY BUT ROW WAS ADDED
        public void AddWorkersCompRiskRateInsuranceCarrier()
        {
            // GIVEN THAT A WC RISK RATE INSURANCE CARRIER IS ADDED, WHEN CHANGED ARE SAVED
            SelectWorkersCompRiskRateCode("WCRR");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.NewEffectiveDateValue, "06/19/2014");
            UltiPro.Pages.WcRiskAddChg.Set(dDown => dDown.InsuranceCarrierDropDown, "WCRCARRIER", SelectOption.Value);
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Save);
            
            // THEN A NEW ROW FOR WCRR IS CREATED IN THE GRID WITH THE NEW EFFECTIVE DATE
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Find(code => code.CodeFilter, FilterType.Is, "WCRR");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Row(1).Click(column => column.Code); // CLICKS THE 2nd ROW
            UltiPro.Pages.WcRiskAddChg.Is(dDown => dDown.InsuranceCarrierDropDown, "innerHTML", "Workers Compensation - Insurance Carrier");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "FL - Florida");
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CodeValue, "innerHTML", "WCRR - Workers Comp Code Description");
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CompanyValue, "innerHTML", "US Payroll Company");
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CurrentEffectiveDateValue, "innerHTML", "6/19/2014");
 
            // TEARDOWN
            Database.Company.Execute("delete from wcrisk where wcrwccode =  'WCRR' and WcrEffDate = '2014-06-19'");
            Thread.Sleep(3000); // NEEDED FOR RECORD TO BE COMPLETELY ERASED
          }

        [Test(Order = 4)]
        public void Verify_EeRate_ErRate_ModifierCanBeEmpty()
        {
            SelectWorkersCompRiskRateCode("WCRR");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.NewEffectiveDateValue, "07/13/2014");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployeeRateTextBox, "");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployerRateTextBox, "");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.ModifierTextBox, String.Empty);
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Save);

            // VERIFY CHANGES IN GRID
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Find(code => code.CodeFilter, FilterType.Is, "WCRR");
            UltiPro.Pages.BusinessRuleDetail.WorkersCompRiskRatesGrid.Row(1).Click(column => column.Code); // CLICK THE 2nd ROW
            UltiPro.Pages.WcRiskAddChg.Like(page => page.CurrentEffectiveDateValue, "innerHTML", "7/13/2014"); 
            UltiPro.Pages.WcRiskAddChg.Is(textBox => textBox.EmployeeRateTextBox, String.Empty);
            UltiPro.Pages.WcRiskAddChg.Is(textBox => textBox.EmployerRateTextBox, "");
            UltiPro.Pages.WcRiskAddChg.Is(textBox => textBox.ModifierTextBox, "");
            
            // TEARDOWN
            Database.Company.Execute("delete from wcrisk where wcrwccode = 'WCRR' and WcrEffDate = '2014-07-13'");
            Thread.Sleep(3000); // NEEDED FOR RECORD TO BE COMPLETELY ERASED
          }
        
        [Test(Order = 5)]
        public void VerifyPercentageCannotBeGreaterThan100()
        {
            SelectWorkersCompRiskRateCode("WCRR");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployeeRateTextBox, "101");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployerRateTextBox, "100.9");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Save);
            UltiPro.Pages.WcRiskAddChg.ErrorMessages.Like(msg => msg.MessageBox, "innerHTML", "Value is not within valid range EmployeeRate");
            UltiPro.Pages.WcRiskAddChg.ErrorMessages.Like(msg => msg.MessageBox, "innerHTML", "Value is not within valid range EmployerRate");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Reset);
        }

        [Test(Order = 6)]
        public void VerifyPercentageCantBeLessThanZero_ButModifierCanBeLessThanZero()
        {
            SelectWorkersCompRiskRateCode("WCRR");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployeeRateTextBox, "-1");
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.EmployerRateTextBox, "-.1");

            // VERIFY MODIFIER CAN BE LESS THAN 0
            UltiPro.Pages.WcRiskAddChg.Set(textBox => textBox.ModifierTextBox, "-.01");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Save);
            UltiPro.Pages.WcRiskAddChg.ErrorMessages.Like(msg => msg.MessageBox, "innerHTML", "Value is not within valid range EmployeeRate");
            UltiPro.Pages.WcRiskAddChg.ErrorMessages.Like(msg => msg.MessageBox, "innerHTML", "Value is not within valid range EmployerRate");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(button => button.Reset);
        }
    }
}